package com.Pratice;

import java.util.Scanner;

public class PREV54 {
	public static void main(String[] args) {
		Scanner scan = new  Scanner(System.in);
		int row = scan.nextInt();
		int col = scan.nextInt();
		int len = scan.nextInt();
		UF uf = new UF(row*col);
		
		for(int i = 0;i < len;i++) {
			int x = scan.nextInt() - 1;
			int y = scan.nextInt() - 1;
			if(!uf.connected(x, y))
				uf.union(x,y );
		}
		System.out.println(uf.count);
	}
}

class UF{
	static int[] parent;
	static int count;
	static int[] size;
	
	public UF(int N) {
		parent = new int[N];
		size = new int[N];
		count = N;
		for (int i = 0; i < N; i++) {
			parent[i] = i;
			size[i] = 1;
		}
	}
	
	public static void union(int p,int q) {
		int rootp = find(p);
		int rootq = find(q);
		if(rootp == rootq)
			return;
		if(size[rootp] > size[rootq]) {
			parent[rootq] = parent[rootp];
			size[rootp] +=size[rootq];
		}
		else {
			parent[rootp] = parent[rootq];
			size[rootq] += size[rootp];
		}
		count--;
	}
	
	public static boolean connected(int p,int q) {
		int rootp = find(p);
		int rootq = find(q);
		return  rootp == rootq;
	}
	
	public static int find(int x) {
		while(parent[x] != x) {
			parent[x] = parent[parent[x]];
			x = parent[x];
		}
		return x;
	}
	
	public static int count() {
		return count;
	}
}